﻿Public Class Frm6UsuariosMenu
    Public Sub cargarRegistros(ByVal fila As Integer)
        Try
            Dim accion As New UsuariosDAO
            'Llenar tabla
            Dim tabla As New DataTable
            tabla = accion.mostrar
            'Comprobar si hay datos
            If Not tabla Is Nothing Then
                'Cargar datos en el grid
                'dgv1.DataSource = tabla
                FormatosGrid.funciones.formatoGrid_Usuarios(DGV1, tabla)
                'Mostrar datos en los controles
                txtIdUsuario.Text = DGV1.Rows(fila).Cells(0).Value
                txtUsuario.Text = DGV1.Rows(fila).Cells(1).Value
                txtContrasenia.Text = DGV1.Rows(fila).Cells(2).Value
                txtRol.Text = DGV1.Rows(fila).Cells(3).Value
                lblIdRol.Text = DGV1.Rows(fila).Cells(4).Value
                'Seleccionar toda la fila
                DGV1.Rows(fila).Selected = True
                DGV1.CurrentCell = DGV1.Rows(fila).Cells(1)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
        End Try
    End Sub
    Private Sub dgv1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV1.CurrentCellChanged
        Try
            If Not DGV1.CurrentCell Is Nothing Then
                With DGV1
                    txtIdUsuario.Text = .Rows(.CurrentCell.RowIndex).Cells(0).Value
                    txtUsuario.Text = .Rows(.CurrentCell.RowIndex).Cells(1).Value
                    txtContrasenia.Text = .Rows(.CurrentCell.RowIndex).Cells(2).Value
                    txtRol.Text = .Rows(.CurrentCell.RowIndex).Cells(3).Value
                    lblIdRol.Text = .Rows(.CurrentCell.RowIndex).Cells(4).Value
                End With
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
              MessageBoxIcon.Warning)
        End Try
    End Sub
    Private Sub Frm6UsuariosMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarRegistros(0)
        txtIdUsuario.Enabled = False
        txtUsuario.Enabled = False
        txtContrasenia.Enabled = False
        txtRol.Enabled = False
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Frm6UsuariosNuevos.Show()
        Me.Close()

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click

        Frm6UsuariosEditar.Show(Me.txtIdUsuario.Text, Me.txtUsuario.Text, Me.txtContrasenia.Text, Me)
        Me.Close()
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dato As New Usuarios
        Dim accion As New UsuariosDAO
        Dim resp As DialogResult
        dato.pIdUsua = txtIdUsuario.Text
        dato.pUsua = txtUsuario.Text
        dato.pContra = txtContrasenia.Text
        dato.pIdRol = lblIdRol.Text


        resp = MessageBox.Show("Desea eliminar el registro " & dato.pUsua, "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If resp = Windows.Forms.DialogResult.Yes Then
            If accion.eliminar(dato) Then
                MessageBox.Show("Registro eliminado", "Borrar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                cargarRegistros(0)
            Else
                MessageBox.Show("No se puede eliminar el registro, contacte al administrador", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End If
    End Sub


End Class